package com.fare.util;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

/**
 * report配置文件
 * @author Administrator
 *
 */
public class ReportProperties {

	private static ReportProperties report=null;
	
	public final static String PATH="path";
	public final static String ADDRESS="address";
	public final static String PORT="port";
	public final static String USERNAME="userName";
	public final static String PASSWORD="password";
	public final static String SAVEPATH="savePath";
	
	private String path=null;
	private String address=null;
	private int port;
	private String userName=null;
	private String password=null;
	private String savePath=null;
	

	private ReportProperties() {
		
	}
	/**
	 * 获取对象，当读取配置文件出错的时候会报异常
	 * @return
	 * @throws Exception
	 */
	public static ReportProperties getInstance() throws Exception{
		if(report==null){
			init();
		}
		return report;
	}
	
	/**
	 *初始化 读取配置文件 
	 */
	private static void init() throws Exception{
		
		FileInputStream fis = null;
		Properties p = null;
		try {
			File f = new File(ReportProperties.class.getClassLoader()
					.getResource("report.properties").getPath());
			fis = new FileInputStream(f);
			p = new Properties();
			p.load(fis);
		} catch (Exception e) {
			throw new Exception("File 'report.properties' not found'");
		}finally{
			try {
				if(fis!=null){
					fis.close();
				}
			} catch (Exception e) {}
		}
		//初始化
		report=new ReportProperties();
		report.path=p.getProperty(ReportProperties.PATH);
		report.address=p.getProperty(ReportProperties.ADDRESS);
		String portStr=p.getProperty(ReportProperties.PORT);
		report.userName=p.getProperty(ReportProperties.USERNAME);
		report.password=p.getProperty(ReportProperties.PASSWORD);
		report.savePath=p.getProperty(ReportProperties.SAVEPATH);
		//避免为空
		report.path=report.path==null?"":report.path; 
		report.address=report.address==null?"":report.address; 
		report.savePath=report.savePath==null?"":report.savePath;
		try{
			report.port=Integer.valueOf(portStr);
		}catch(Exception e){
			report.port=22;
		}
		
		report.userName=report.userName==null?"":report.userName; 
		report.password=report.password==null?"":report.password; 
		
	}
	
	
	/**
	 * 只提供基本get，不提供set
	 * @return
	 */
	
	public String getPath() {
		if(!path.endsWith("/")) path += "/";
		return path;
	}
	
	public String getAddress() {
		return address;
	}
	
	public int getPort() {
		return port;
	}
	
	public String getUserName() {
		return userName;
	}

	public String getPassword() {
		return password;
	}
	public String getSavePath() {
		return savePath;
	}
	

	

	

	
	
	
	
	
}
